package com.kuaiyiwang.springcloud.controller;

import com.kuaiyiwang.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
//@DefaultProperties(defaultFallback ="payment_Global_FallbackMethod" )
public class OrderHystrixController {

    private final Logger logger = LoggerFactory.getLogger(OrderHystrixController.class);
    @Autowired
    PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_ok(id);
        logger.info("******result:{}",result);
        return  result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value="1500")
//    })
 //   @HystrixCommand  //什么都不加，执行全局的，如果有加执行指定的
    public String paymentInfo_timeout(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_timeout(id);
        logger.info("******result:{}",result);
        return  result;
    }

    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "调用接口中超时或异常，当前线程池的名字："+Thread.currentThread().getName();
    }

    public String payment_Global_FallbackMethod(){
        return "Global异常处理信息，请洗脸后再试";
    }

}
